﻿using thinger.ControlLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KYJScadaProModbusRtu
{
    public partial class FrmModify : Form
    {   
        public FrmModify(string titileName,string varName,string currentValue)
        {
            InitializeComponent();

            this.lbl_titleName.Text = titileName;
            this.lbl_CurrentValue.Text = currentValue;
            this.BindVarName = varName;

            this.txt_Modify.Focus();

        }

        private string BindVarName;

        #region 窗体拖动
        public static Point CPoint;

        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            CPoint = new Point(-e.X, -e.Y);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Point myPosittion = Control.MousePosition;//获取当前鼠标的屏幕坐标
                myPosittion.Offset(CPoint.X, CPoint.Y);//重载当前鼠标的位置
                this.DesktopLocation = myPosittion;//设置当前窗体在屏幕上的位置
            }
        }
        #endregion

        private void btn_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Ok_Click(object sender, EventArgs e)
        {
          var result= CommomMethods.CommonWrite(BindVarName, this.txt_Modify.Text.Trim());
            if(result.IsSuccess)
            {
                CommomMethods.AddOPLog(false, "修改参数成功，参数名称：" + this.lbl_titleName.Text + ",原值：" + this.lbl_CurrentValue.Text + ",修改：" + this.txt_Modify.Text.Trim());
                this.Close();
            }
            else
            {
                CommomMethods.AddOPLog(false, "修改参数失败:"+result.Message +",参数名称：" + this.lbl_titleName.Text );
                MessageBox.Show("修改参数失败:" + result.Message);
            }
        }

        private void txt_Modify_Click(object sender, EventArgs e)
        {
            //如果存在就置前，否则就创建
            if (this.Controls["NumKeyboary"] != null)
            {
                //置于最前面
                this.Controls["NumKeyboary"].BringToFront();
            }
            else
            {
                NumKeyboard numKeyboard = new NumKeyboard();

                //控制控件显示在窗体的中间
                numKeyboard.Location = new Point((this.Width - numKeyboard.Width) / 2, (this.Height - numKeyboard.Height) / 2);

                //给控件取个名称
                numKeyboard.Name = "NumKeyboary";

                //设置限制值
                numKeyboard.MaxValue = 100.0f;
                numKeyboard.MinValue = -100.0f;

                //设置默认值
                numKeyboard.InitialValue = this.lbl_CurrentValue.Text;

                //添加事件
                numKeyboard.ESCClick += NumKeyboard_ESCClick;
                numKeyboard.EnterClick += NumKeyboard_EnterClick;

                //添加窗体容器
                this.Controls.Add(numKeyboard);

                //置于最前面
                numKeyboard.BringToFront();
            }      
        }

        private void NumKeyboard_EnterClick(object sender, EventArgs e)
        {
          if(sender is NumKeyboard keyboard)
            {
                this.txt_Modify.Text = keyboard.ResultValue;
                this.Controls.Remove(keyboard);
            }
        }

        private void NumKeyboard_ESCClick(object sender, EventArgs e)
        {
            if (sender is NumKeyboard keyboard)
            {
                this.Controls.Remove(keyboard);
            }
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
